﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Demo.Mmose.Core.Common.SafeCollections;
using Demo.Mmose.Core.Common;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Spell
{
    
    /// <summary>
    /// 技能链
    /// </summary>
    public class SpellLine : IEnumerable<Spell>
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="spells"></param>
        public SpellLine()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="spells"></param>
        public SpellLine( long spellLineId )
        {
            m_Serial = spellLineId;
        }

        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS Serial属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private Serial m_Serial = new Serial();
        #endregion
        /// <summary>
        /// 唯一序列号GUID
        /// </summary>
        public Serial Serial
        {
            get { return m_Serial; }
            set { m_Serial = value; }
        }

        #endregion

        #region zh-CHS Name属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private string m_Name = string.Empty;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        #endregion

        #region zh-CHS FirstSpell属性 | en Public Properties

        /// <summary>
        /// 
        /// </summary>
        public Spell FirstSpell
        {
            get 
            {
                LinkedListNode<Spell> firstSpell = m_SpellLine.First;

                if ( firstSpell == null )
                    return null;
                else
                    return firstSpell.Value;
            }
        }

        #endregion

        #region zh-CHS LastSpell属性 | en Public Properties

        /// <summary>
        /// 
        /// </summary>
        public Spell LastSpell
        {
            get 
            {
                LinkedListNode<Spell> lastSpell = m_SpellLine.Last;

                if ( lastSpell == null )
                    return null;
                else
                    return lastSpell.Value;
            }
        }

        #endregion
        
        #endregion

        #region zh-CHS 共有方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeLinkedList<Spell> m_SpellLine = new SafeLinkedList<Spell>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="spell"></param>
        public void AddSpell( Spell spell )
        {
            LinkedListNode<Spell> thisLinkedListNode = m_SpellLine.AddLast( spell );

            spell.SpellLine = this;
            spell.ThisLinkedListNode = thisLinkedListNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spell"></param>
        public void RemoveSpell( Spell spell )
        {
            if ( m_SpellLine.Remove( spell ) == true )
            {
                spell.SpellLine = null;
                spell.ThisLinkedListNode = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Spell[] ToArray()
        {
            return m_SpellLine.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<SpellT> ToArray<SpellT>() where SpellT : Spell
        {
            Spell[] tempSpellArray = this.ToArray();
            if ( tempSpellArray == null )
                yield break;

            for ( int iIndex = 0; iIndex < tempSpellArray.Length; iIndex++ )
            {
                SpellT spell = tempSpellArray[iIndex] as SpellT;
                if ( spell == null )
                    continue;
                else
                    yield return spell;
            }
        }

        #endregion

        #region zh-CHS 方法覆盖 | en Override Methods
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
		{
			return m_Name;
		}
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<Spell> GetEnumerator()
        {
            return m_SpellLine.GetEnumerator();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }

}
#endregion